<?php
class AgentsController extends AppController {

	var $name = 'Agents';
	var $helpers = array('Html', 'Form', 'Javascript','Flash');



	function admin_index() {
	if(isset($this->params['admin']) && $this->params['admin']) {
		// check that a user is logged in
		    $this->check_user();
	    }
		$this->layout = "admin";
		$this->Agent->recursive = 0;
		$this->set('agents', $this->paginate());
	}

	function admin_view($id = null) {
			if(isset($this->params['admin']) && $this->params['admin']) {
		// check that a user is logged in
		    $this->check_user();
	    }
	$this->layout = "admin";
		if (!$id) {
			$this->Session->setFlash(__('Invalid Agent.', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->set('agent', $this->Agent->read(null, $id));
	}

	function admin_add() {
	if(isset($this->params['admin']) && $this->params['admin']) {
		// check that a user is logged in
		    $this->check_user();
	    }
	$this->layout = "admin";
		if (!empty($this->data)) {
			$this->Agent->create();
			if ($this->Agent->save($this->data)) {
				$this->Session->setFlash(__('The Agent has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Agent could not be saved. Please, try again.', true));
			}
		}
	}

	function admin_edit($id = null) {
			if(isset($this->params['admin']) && $this->params['admin']) {
		// check that a user is logged in
		    $this->check_user();
	    }
	$this->layout = "admin";
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Agent', true));
			$this->redirect(array('action'=>'index'));
		}
		if (!empty($this->data)) {
			if ($this->Agent->save($this->data)) {
				$this->Session->setFlash(__('The Agent has been saved', true));
				$this->redirect(array('action'=>'index'));
			} else {
				$this->Session->setFlash(__('The Agent could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Agent->read(null, $id);
		}
	}

	function admin_delete($id = null) {
			if(isset($this->params['admin']) && $this->params['admin']) {
		// check that a user is logged in
		    $this->check_user();
	    }
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for Agent', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Agent->del($id)) {
			$this->Session->setFlash(__('Agent deleted', true));
			$this->redirect(array('action'=>'index'));
		}
	}
	
	function admin_logout() 
	{
	
	// delete the user session
	$this->Session->delete('Agent');
	// redirect to posts index page
	$this->Session->setFlash('Ha salido satisfactoriamente.');
	$this->redirect('/admin/agents/login');
	
    }
	
	
	function admin_login()
    {
    
    $this->layout = "adminlogin";
    $this->recursive = 0;
	if(!empty($this->data))
	
	 {

		// find the user in the database
		    $dbuser = $this->Agent->findByEmail($this->data['Agent']['email']);

		    if(!empty($dbuser) && ($dbuser['Agent']['password'] == $this->data['Agent']['password'])) 
		    {
			    // write the username to a session
			    $this->Session->write('Agent', $dbuser);
			    // redirect the user
			    $this->Session->setFlash('Se ha autenticado correctamente.');
			    $this->redirect('/admin/Clasificados/');
		    } 
		    else 
		    {
			    $this->set('error', 'Su Nombre de usuario o clave son erradas.');
			    $this->redirect('/admin/agents/login');
		    }
        }
	}

}
?>
